import { useMount } from "ahooks"
import { useLocation } from "react-router-dom"
import useCommonModel from "../../../model/useCommonModel"
import useIntentModel from "../../../model/useIntentModel"
import { getUrlParams } from "../../../utils"

interface UiState {
    isViewMode: boolean
    isPreSecondSignMode: boolean
    isAlterTenantMode: boolean
    isAlterPlanMode: boolean
}

interface UrlParams {
    entryNo: string
    intentionNo: string
    isAlterTenant: string
    isPreSecondSign: string
    isAlterPlan: string
    type: string
    isRepaying: string
}

export const useGetUrlParams = (): UrlParams => {
    const location = useLocation()
    const urlParams = getUrlParams(location.search)
    return urlParams
}

export const useInputViewInit = () => {
    const urlParams = useGetUrlParams()
    const { entryNo } = urlParams
    const { fetchEntryInfo } = useIntentModel()
    const { fetchAreaData } = useCommonModel()
    useMount(() => {
        fetchEntryInfo({ entryNo })
        fetchAreaData(null)
    })
}

export const useGetUiState = (): UiState => {
    const urlParams = useGetUrlParams()
    const { type, isAlterTenant, isPreSecondSign, isAlterPlan } = urlParams
    const isViewMode = type === 'view'
    const isPreSecondSignMode = isPreSecondSign === 'Y'
    const isAlterTenantMode = isAlterTenant === 'Y'
    const isAlterPlanMode = isAlterPlan === 'Y'
    return { isViewMode, isPreSecondSignMode, isAlterTenantMode, isAlterPlanMode }
}